Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
TypeScript Type Hoisting "chicken or egg" problem

Before someone marks this as a duplicate, I checked 2 other questions that didn't actually solve anything. They were only ideas for alternatives ways to do the exact problem, but didn't show a solution to my problem.

When making an array in TypeScript, I assigned the type any[] to it, since I haven't imported the type of the array yet. However, I have to pass this array to a require statement to make class methods work on this particular array.

If I make the array first, (using any[]) then I won't be able to type the array.

let blockchain: any[] = [];

// Pass array to import classes and functions
const {/* stuff to import */} = require("./otherfile.js")(blockchain);
type BlockType = InstanceType<typeof Block>;

// How do I set blockchain's type to BlockType[]?

However, if I import the classes first, I won't be able to pass the array to give the correct array to class methods

// Pass array to import classes and functions
const {/* stuff to import */} = require("./otherfile.js")(blockchain); // error
type BlockType = InstanceType<typeof Block>;

let blockchain: BlockType[] = [];

Importing the module twice, first with an empty array just to obtain the class, is not an option for me, since the original import's class methods will push data to the empty array and it'll be pointless.

EDIT: To be more specific about the module classes, this is what happens inside the other file.

function exp(blockchain: any[]): Object {
    class Block {
        // public x: number, etc...
        
        constructor(
            // stuff...
        }

        static generate(/* params */): Block {
            // do stuff, edit properties...

            let block = new Block(/* args */);
            blockchain.push(block); // Array that was passed to module
            return block;
        }
    }
    
    return {
        Block: Block
    }
}

Scotty Jamison's suggestion wouldn't work here because the Block class from import #1 wouldn't be the same as the one from import #2, and their methods would push to different arrays.

let {Block1} = require("./otherfile.js")([]);
let blockchain: Block1[] = [];
let {Block2} = require("./otherfile.js")(blockchain);

Block1.generate("yo");
Block2.generate("foobar");

console.log(blockchain.length); // 1
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda